package com.teadent.dao;

import com.teadent.bean.Person;
import com.teadent.bean.Task;
import com.teadent.util.ConnectionFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Deal {
    public static Person getSelf(String sql) {
        Connection connection= ConnectionFactory.getConnection();
        PreparedStatement pre=null;
        ResultSet res=null;
        Person newPerson=new Person();
        try{
            pre=connection.prepareStatement(sql);

            res= pre.executeQuery();
            while (res.next()) {
                newPerson.setUserName(res.getString("userName"));
                newPerson.setUserId(res.getString("userId"));
                newPerson.setUserAccount(res.getString("userAccount"));
                newPerson.setUserPassword(res.getString("userPassword"));
                newPerson.setUserIdentify(res.getInt("userIdentify"));
                newPerson.setUserSex(res.getString("userSex"));
                newPerson.setUserEmail(res.getString("userEmail"));
                newPerson.setUserOthername(res.getString("userOthername"));
            }
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }

        return newPerson;
    }

    public static void deal(String sql) {
        Connection connection= ConnectionFactory.getConnection();
        PreparedStatement pre=null;
        ResultSet res=null;
        try{
            pre=connection.prepareStatement(sql);
            pre.execute();
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
    }

    public static Task getTask(String sql) {
        Connection connection= ConnectionFactory.getConnection();
        PreparedStatement pre=null;
        ResultSet res=null;
        Task newTask=new Task();
        try{
            pre=connection.prepareStatement(sql);
            res= pre.executeQuery();
            while (res.next()) {
                newTask.setTaskName(res.getString("taskName"));
                newTask.setTaskRequirement(res.getString("taskRequirement"));
                newTask.setTaskDate(res.getString("taskDate"));
                newTask.setTaskAccount(res.getInt("taskAccount"));
            }
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
        return newTask;
    }
}
